package com.dukk.espark;

import com.dukk.espark.core.Engine;
import com.dukk.espark.core.EngineCore;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.*;


/**
 * spark 统计引擎启动入口类
 *
 * @version 2020-08-05
 * @author shengshi_feiyang@yeah.net
 */
public class EngineApp implements Serializable {
    private static final long serialVersionUID = 415719788292899541L;

    private static final Logger logger = LoggerFactory.getLogger(EngineApp.class);

    /**
     * 入口使用说明:
     *  开发者根据debug需求，自行修改$userAppName  engineCore.start(args, "$userAppName");
     */
    public static void main(String[] args)  {
        if(args.length > 0){//集群模式
            new EngineCore().start(args, "");
        }else { //本地debug模式 开发自行扩展启动入口调试方法
            StringBuffer buf = new StringBuffer();

            Reflections reflections = new Reflections("com.dukk.espark.*");

            Set<Class<?>> engineClasses = reflections.getTypesAnnotatedWith(Engine.class);

            Map<Integer, String> engineMap = new HashMap<>();
            int i = 1;
            for(Class engine : engineClasses){
                String className = engine.getSimpleName();
                engineMap.put(i, className);
                buf.append("任务"+className+"序号 = ["+i+"]\n");
                i++;
            }
            buf.append("请输入要debug的任务序号,enter键确认:");
            System.out.print(buf);// 提示输入启动的任务序号
            Scanner scanner = new Scanner(System.in);// 获得控制台输入流
            int engineId = scanner.nextInt();// 获得用户输入

            if(null != engineMap.get(engineId)){
                new EngineCore().start(args, engineMap.get(engineId));
            }else {
                System.out.println("输入的序号无对应的APP");
            }

        }

    }
}
